package com.zklh.book.system.rest;

import com.zklh.book.system.common.model.RestResponse;
import com.zklh.book.system.enums.UserRoleEnum;
import com.zklh.book.system.model.Book;
import com.zklh.book.system.model.User;
import com.zklh.book.system.service.BookService;
import com.zklh.book.system.service.UserService;
import com.zklh.book.system.service.impl.BookServiceImpl;
import com.zklh.book.system.service.impl.UserServiceImpl;
import com.zklh.book.system.util.BeanFactory;

import java.util.List;

/**
 * 图书controller
 *
 * @author 坐看落花
 * @version 1.0
 * @date 2021/3/20 19:26
 */
public class BookController {

    private BookService bookService = BeanFactory.getBean(BookServiceImpl.class);
    private UserService userService = BeanFactory.getBean(UserServiceImpl.class);

    /**
     * 获取图书列表
     * @param token
     * @return
     */
    public RestResponse<List<Book>> list(String token){
        // 如果未登录不允许查询
        if (!userService.logined(token)) {
            RestResponse.error(503, "用户未登录, 请重新登录!");
        }
        List<Book> books = bookService.getAll();
        return RestResponse.success(books);
    }

    /**
     * 获取已借图书列表
     * @param token
     * @return
     */
    public RestResponse<List<Book>> mylist(String token){
        //如果未登录禁止查询
        if (!userService.logined(token)){
            RestResponse.error(503,"用户未登录,请重新登录!");
        }
        User user = userService.getByToken(token);
        List<Book> books = bookService.getMy(user);
        return RestResponse.success(books);
    }


    /**
     * 借书
     * @param bookCode
     * @return
     */
    public RestResponse<String> borrow(String token,String bookCode) {
        // 如果未登录不允许查询
        if (!userService.logined(token)) {
            RestResponse.error(503, "用户未登录, 请重新登录!");
        }
        // 借书
        User user = userService.getByToken(token);
        boolean borrowed =false;
        try {
            borrowed = bookService.borrow(user,bookCode);
        } catch (Exception e) {
            return RestResponse.error(500, e.getMessage());
        }
        if (!UserRoleEnum.CONSUMER.getKey().equals(user.getRole())) {
            return RestResponse.error(505,"权限不足!");
        }
        if (borrowed) {
            return RestResponse.success("借书成功!");
        } else {
            return RestResponse.error(500,"借书失败!");
        }
    }


    /**
     * 还书
     * @param token
     * @param bookCode
     * @return
     */
    public RestResponse<String> back(String token,String bookCode) {
        // 如果未登录不允许查询
        if (!userService.logined(token)) {
            RestResponse.error(503, "用户未登录, 请重新登录!");
        }
        // 借书
        User user = userService.getByToken(token);
        if (!UserRoleEnum.CONSUMER.getKey().equals(user.getRole())) {
            return RestResponse.error(505,"权限不足!");
        }
        try {
            bookService.back(user,bookCode);
        } catch (Exception e) {
            return RestResponse.error(500, e.getMessage());
        }
        return RestResponse.success("还书成功!");
    }

    /**
     * 新增图书
     * @param token
     * @param book
     * @return
     */
    public RestResponse<String> add(String token, Book book) {
        if (!userService.logined(token)) {
            RestResponse.error(503, "用户未登录, 请重新登录!");
        }
        User user = userService.getByToken(token);
        if (!UserRoleEnum.ADMIN.getKey().equals(user.getRole())) {
            return RestResponse.error(505,"权限不足!");
        }
        boolean success = false;
        try {
            success = bookService.add(book);
        } catch (Exception e) {
            return RestResponse.error(500,e.getMessage());
        }
        if (success) {
            return RestResponse.success("新增成功!");
        } else {
            return RestResponse.success("新增失败!");
        }
    }

    /**
     * 上架书
     * @param token
     * @param bookCode
     * @return
     */
    public RestResponse<String> enable(String token, String bookCode) {
        if (!userService.logined(token)) {
            RestResponse.error(503, "用户未登录, 请重新登录!");
        }
        User user = userService.getByToken(token);
        if (!UserRoleEnum.ADMIN.getKey().equals(user.getRole())) {
            return RestResponse.error(505,"权限不足!");
        }
        boolean success =false;
        try {
            success = bookService.enable(bookCode);
        } catch (Exception e) {
            return RestResponse.error(500, e.getMessage());
        }
        if (success) {
            return RestResponse.success("操作成功!");
        } else {
            return RestResponse.success("操作失败!");
        }
    }

    /**
     * 下架书
     * @param token
     * @param bookCode
     * @return
     */
    public RestResponse<String> disable(String token, String bookCode) {
        if (!userService.logined(token)) {
            RestResponse.error(503, "用户未登录, 请重新登录!");
        }
        User user = userService.getByToken(token);
        if (!UserRoleEnum.ADMIN.getKey().equals(user.getRole())) {
            return RestResponse.error(505,"权限不足!");
        }
        boolean success = false;

        try {
            success = bookService.disable(bookCode);
        } catch (Exception e) {
            return RestResponse.error(500, e.getMessage());
        }
        if (success) {
            return RestResponse.success("操作成功!");
        } else {
            return RestResponse.success("操作失败!");
        }
    }

    /**
     * 删除树
     * @param token
     * @param bookCode
     * @return
     */
    public RestResponse<String> delete(String token, String bookCode) {
        if (!userService.logined(token)) {
            RestResponse.error(503, "用户未登录, 请重新登录!");
        }
        User user = userService.getByToken(token);
        if (!UserRoleEnum.ADMIN.getKey().equals(user.getRole())) {
            return RestResponse.error(505,"权限不足!");
        }
        boolean success = false;

        try {
            success = bookService.delete(bookCode);
        } catch (Exception e) {
            return RestResponse.error(500, e.getMessage());
        }
        if (success) {
            return RestResponse.success("操作成功!");
        } else {
            return RestResponse.success("操作失败!");
        }
    }
    /**
     * 更改书名称
     * @param token
     * @param bookCode
     * @return
     */
    public RestResponse<String> changecode(String token, String bookCode, String name) {
        if (!userService.logined(token)) {
            RestResponse.error(503, "用户未登录, 请重新登录!");
        }
        User user = userService.getByToken(token);
        if (!UserRoleEnum.ADMIN.getKey().equals(user.getRole())) {
            return RestResponse.error(505,"权限不足!");
        }
        boolean success =false;
        try {
            success = bookService.ccode(bookCode,name);
        } catch (Exception e) {
            return RestResponse.error(500, e.getMessage());
        }
        if (success) {
            return RestResponse.success("操作成功!");
        } else {
            return RestResponse.success("操作失败!");
        }
    }
    /**
     * 获取一本书的全部内容
     * @param token
     * @param bookCode
     * @return
     */
    public RestResponse<Book> onelist(String token, String bookCode) {
        if (!userService.logined(token)) {
            RestResponse.error(503, "用户未登录, 请重新登录!");
        }
        User user = userService.getByToken(token);
        if (!UserRoleEnum.ADMIN.getKey().equals(user.getRole())) {
            return RestResponse.error(505,"权限不足!");
        }

        Book book= bookService.getone(bookCode);
        return RestResponse.success(book);
    }
    /**
     * 改变书的编号
     * @param bookCode,name
     */
    public RestResponse<String> changenumber(String bookCode,String name){

        boolean success =false;
        try {
            success = bookService.numbering(bookCode,name);
        } catch (Exception e) {
            return RestResponse.error(500, e.getMessage());
        }
        if (success) {
            return RestResponse.success("操作成功!");
        } else {
            return RestResponse.success("操作失败!");
        }
    }

    /**
     * 改变书本剩余的数目
     * @param bookCode,sum
     */
    public RestResponse<String> changeremain(String bookCode,Integer sum){
        boolean success =false;
        try {
            success = bookService.numbers(bookCode,sum);
        } catch (Exception e) {
            return RestResponse.error(500, e.getMessage());
        }
        if (success) {
            return RestResponse.success("操作成功!");
        } else {
            return RestResponse.success("操作失败!");
        }
    }

}
